# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-lapis-eswidget
_rockname=lapis-eswidget
pkgver=1.4.0
pkgrel=1
pkgdesc="Lapis widget class library"
url="https://github.com/leafo/lapis-eswidget"
arch="noarch !ppc64le !riscv64 !loongarch64" #blocked by lua-lapis, which requires lua-etlua
license="MIT"
makedepends="luarocks"
source="$pkgname-$pkgver.tar.gz::https://github.com/leafo/$_rockname/archive/v$pkgver.tar.gz
	LICENSE"
builddir="$srcdir/$_rockname-$pkgver"
checkdepends="lua-tableshape lua-busted"
depends="lua-argparse"
subpackages="$pkgname-doc"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	depends="$depends lua$_v lua$_v-lapis"
	checkdepends="$checkdepends lua$_v-tableshape lua$_v-busted"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree="./build" \
			--deps-mode=none \
			"$_rockname-dev-1.rockspec"

		rm "./build/lib/luarocks/rocks-$lver/manifest"
	done
}

package() {
	mkdir -p "$pkgdir"

	install -Dm644 "$srcdir"/LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

check() {
	mkdir .bin

	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver..."
		ln -sf "/usr/bin/lua$lver" ".bin/lua"
		LUA_PATH="$builddir/?.lua;;" \
		PATH="$builddir/.bin:$PATH" \
		lua "/usr/bin/busted"
	done
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver lua$lver-lapis"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_rockname/$pkgver-1"

	local path; for path in "lib/luarocks/rocks-$lver" "share/lua/$lver"; do
		mkdir -p "$subpkgdir/usr/${path%/*}"
		mv "$builddir/build/$path" "$subpkgdir/usr/$path/"
	done

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
784e847c9aba5071a38046769ea72892af3baed8ac4826f0e6ce001885c2125fa7b722e359b57bbed579a79b3539a5254f76c2c5e7862cf2ae446da322ab717c  lua-lapis-eswidget-1.4.0.tar.gz
8d23c78a24f99ffc27adbc75402368ea7445034c31124966fed9ccf7e0fbf31358b6ff09719a390452145c33040174b3cf8cb89e3eb54804eb7d6c2c86abff2f  LICENSE
"
